# 标准库导入
import traceback
import pickle
import string
import time
import logging
import re
import sys
import os
import subprocess
import threading
import tempfile
import shutil
import uuid
import json
from pathlib import Path
from types import SimpleNamespace

# 第三方库导入
import torch
import numpy as np
import pandas as pd
import uvicorn
from pydantic import BaseModel, Field
from typing import Optional, Union, Dict, List
try:
    from typing import Literal
except ImportError:
    from typing_extensions import Literal
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from natsort import natsorted
from sklearn.metrics import mean_squared_error
from natsort import natsorted


app = FastAPI(title="接口说明文档", description="当前支持如下接口", version="0.0.1")


class SampleRequest(BaseModel):
    uuid: Optional[str] = None
    method: Literal["LHS", "Random", "FullFactorial", "MC"]
    num_samples: int = Field(gt=0)
    sampleinfo: Union[str, dict]
    output_dir: str = Field(...,
                            description="自定义输出目录，采样点文件将保存到该目录下的input_data子目录")
    cae_type: Optional[str] = None


if __name__ == "__main__":
    # Console-only logging config
    uvicorn_log_config = {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "default": {
                "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
            },
        },
        "handlers": {
            "console": {
                "formatter": "default",
                "class": "logging.StreamHandler",
                "stream": "ext://sys.stderr",
            },
        },
        "root": {
            "level": "INFO",
            "handlers": ["console"],
        },
    }

    logging.config.dictConfig(uvicorn_log_config)
    logging.info("启动 uvicorn 服务器...")

    uvicorn.run(
        "GeneralRomApi:app",
        host="0.0.0.0",
        port=3027,
        reload=False,
        log_config=uvicorn_log_config,
        access_log=False,
    )
